var loop;
var msgMap = new Map();
var userId = 513822;
function createSocket(uid) {
  userId = uid;
  //判断当前浏览器是否支持WebSocket
  if ("WebSocket" in window) {
    websocket = io.connect(
      "ws://127.0.0.1:8036/im/agent?userid="+ userId+"&origin=customer&token=12321312321321&admin=admin&scene=call&targetType=1",
      { transports: ["websocket", "xhr-polling", "jsonp-polling"] }
    );
    loop = window.setInterval(function () {
      websocket.emit("event",{
        userId: userId,
        event:"PING"
      });
    }, 1000 * 10);
  } else {
    layer.msg(unSupportWsMsg, { anim: 6 });
  }

  websocket.on("connect", function () {
    console.log("on connect ...");
  });

  websocket.on("sessions", function (event) {
    let sessions = JSON.parse(event.statusMessage);
    users = sessions;

    users.forEach((item, index, array) => {
      if (index == 0) {
        currentUser = item;
      }
      msgMap.set(item.targetId, new Array());
      $("#users").append(
        ' <a id="' +
          item.targetId +
          '" href="javascript:chooceUser(' +
          item.targetId +
          ')" class="canvi-user-info__meta" style="font-size:16px"> 用户：' +
          item.targetNickname +
          "</a><br>"
      );
    });
    chooceUser(currentUser.targetId);
  });

  websocket.on("new", function (event) {
    let statusMessage = JSON.parse(event.statusMessage);

    let exists = false;
    users.forEach((item, index, array) => {
      if (statusMessage.targetId == item.targetId) {
        exists = true;
      }
    });

    if (!exists) {
      currentUser = {
        targetId: statusMessage.targetId,
        sessionId: statusMessage.sessionId,
        scene: statusMessage.scene,
        userId: statusMessage.userId,
        targetType: statusMessage.targetType,
      };
      users.push(currentUser);
      msgMap.set(currentUser.targetId, new Array());
      $("#users").append(
        ' <a id="' +
          currentUser.targetId +
          '" href="javascript:chooceUser(' +
          currentUser.targetId +
          ')" class="canvi-user-info__meta" style="font-size:16px"> 用户：' +
          statusMessage.targetNickname +
          "</a><br>"
      );
      chooceUser(currentUser.targetId);
    }
  });

  websocket.on("hismessage", function (data) {
    let statusMessage = JSON.parse(data.statusMessage);
    statusMessage.forEach((element) => {
      putMsg(element);
    });
  });

  //接收到消息的回调方法
  websocket.on("message", function (event) {
    if (
      event.sendUser == currentUser.targetId ||
      event.sendUser == currentUser.userId
    ) {
      putMsg(event);
    }
  });
}

function putMsg(data) {
  var msg = data.message;
  var nick = data.sendNickname;
  switch (data.msgtype) {
    case "init":
      getOnlion(document.getElementById("room").value);
      layer.msg(msg);
      break;
    case "text":
      if (data.sendUser == currentUser.userId) {
        setMessageInnerHTML2(nick, text2Emoji2(msg));
      } else {
        setMessageInnerHTML(nick, text2Emoji2(msg));
      }
      break;
    case "img":
      if (data.sendUser == currentUser.userId) {
        setImgInnerHTML2(nick, msg);
      } else {
        setImgInnerHTML(nick, msg);
      }
      break;
    case "file":
      if (data.sendUser == currentUser.userId) {
        setFileInnerHTML2(nick, msg, data.filename);
      } else {
        setFileInnerHTML(nick, msg, data.filename);
      }
      break;
    case "bing":
      document.getElementById("userId").value = data.id;
      $("body").css("background-image", "url(" + msg + ")");
      break;
    default:
      break;
  }
}

function chooceUser(uid) {
  emptyScreen();
  users.forEach((item, index, array) => {
    if (uid == item.targetId) {
      currentUser = item;
      $("#" + item.targetId).css("color", "green");
      websocket.emit("event", {
        sendUser: currentUser.userId,
        targetId: currentUser.targetId,
        event: "MESSAGES",
      });

      // msgs.forEach((msg, i, ar) => {
      //   putMsg(msg);
      // });
    } else {
      $("#" + item.targetId).css("color", "white");
    }
  });
}
